package project1.controller;

import com.google.gson.Gson;

import project1.model.Admin;
import project1.model.Result;
import project1.model.bo.AdminAddBO;
import project1.model.bo.AdminLoginBO;
import project1.model.bo.AdminSearchBO;
import project1.service.AdminService;
import project1.service.AdminServiceImpl;
import project1.view.AdminLoginVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import project1.utils.HtttpUtils;
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    Gson gson = new Gson();
    private AdminService adminService = new AdminServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 既然当前servlet可以处理诸如登陆，查询所有admin，删除admin等操作
         * 那么怎么知道当前请求是什么呢？
         */
        //返回一个URI/api/admin/admin/
        String requestURI = request.getRequestURI();
        //System.out.println(requestURI);
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("login".equals(action)){
            login(request,response);
        }
        if("getSearchAdmins".equals(action)){
            getSearchAdmins(request,response);
        }
        if ("addAdminss".equals(action)){
            addAdmin(request,response);
        }
        if ("updateAdminss".equals(action)){
            updateAdmins(request,response);
        }

    }

    /**
     * 更改管理员账户信息
     * @param request
     * @param response
     */
    private void updateAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HtttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody,Admin.class);//需要把Json对象转成Admin类
        System.out.println(requestBody);
        adminService.updateAdmin(admin);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    /**
     * 添加一个管理员账户
     * @param request
     * @param response
     */
    private void addAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HtttpUtils.getRequestBody(request);
        AdminAddBO adminAddBO = gson.fromJson(requestBody,AdminAddBO.class);
        System.out.println(adminAddBO);
        adminService.addAdmin(adminAddBO);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    /**
     * 条件查询admin管理员账户信息
     * 1.浏览器向8084发送一个请求，请求体启动携带了用户账户昵称参数（json字符串）
     * 2.查询数据库，返回满足条件的数据
     * 3.做出响应
     *
     *
     * @param request
     * @param response
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HtttpUtils.getRequestBody(request);
        AdminSearchBO searchBO = gson.fromJson(requestBody, AdminSearchBO.class);
        List<Admin> admins = adminService.getSearchAdmins(searchBO);
        response.getWriter().println(gson.toJson(Result.ok(admins)));
    }

    /**
     * 管理员登录的具体业务逻辑
     * 1.浏览器向8084发送一个请求，请求体启动携带了用户名密码参数（json字符串）
     * 2.查询数据库，校验当前用户名密码是否正确
     * 3.根据结果返回不同的响应
     * @param request
     * @param response
     */

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //取出请求参数--取出请求体里面的数据
    //此时无法使用request.getParameter key=value&key1=value
        //返回一个java对象
        String requestBody = HtttpUtils.getRequestBody(request);
        //json对象变成java对象
        AdminLoginBO loginBO = gson.fromJson(requestBody, AdminLoginBO.class);
       // System.out.println(loginBO);
        Admin login = adminService.login(loginBO);
        if (login != null){
            AdminLoginVo loginVo = new AdminLoginVo();
            loginVo.setToken(login.getNickname());
            loginVo.setName(login.getNickname());
            //System.out.println(Result.ok(loginVo));
            //java对象变成一个json对象,返回给前端
            response.getWriter().println(gson.toJson(Result.ok(loginVo)));
//            response.getWriter().println("登陆成功");
//            response.getWriter().println(request.getRequestURI());
//            response.getWriter().println(request.getContextPath());
//            response.getWriter().println(request.getRequestURL());
        }else {
             response.getWriter().println(Result.error("用户名错误或者密码错误"));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String id = request.getParameter("id");
        //System.out.println("id = " +id);
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("allAdmins".equals(action)){
            allAdmins(request,response);
        }
        if (("deleteAdmins").equals(action)){
            deleteAdmin(request,response,id);
        }
        if ("getAdminsInfo".equals(action)){
            modifyAdminsInfo(request,response);
        }
    }

    /**
     * 修改用户信息
     * @param request
     * @param response
     */
    private void modifyAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Admin admin = adminService.getAdminInfo(id);
        //System.out.println(admin);
        response.getWriter().println(gson.toJson(Result.ok(admin)));
    }

    /**
     * 删除用户名
     * @param request
     * @param response
     * @param id
     */
    private void deleteAdmin(HttpServletRequest request, HttpServletResponse response, String id) throws IOException {
        adminService.deleteAdmin(id);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    /**
     * 显示所有的admin账户信息
     * 1.查询数据库，返回数据
     * 2.作出响应
     * @param request
     * @param response
     */
    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
    List<Admin> adminList = adminService.allAdmins();
    Result result = new Result();
    result.setCode(0);
    result.setData(adminList);
    response.getWriter().println(gson.toJson(result));
    }
}
